from direct.showbase.ShowBase import ShowBase
from Unit import *
from xml.dom import minidom

class Player:
    def __init__(self):
        self.Units = []
    
    def UpdateUnits(self):
        to_delete = []
        for unit in self.Units:
            if unit.size <= 0:
                to_delete.append(unit)
        for unit in to_delete:
            self.DeleteUnit(unit)

    def AddUnit(self, unit):
        #unit = Unit()                   
        #unit.Initialize(size,10,10,5,hex, self, 0, model)
        unit.model.reparentTo(render)
        self.Units.append(unit)

    def DeleteUnit(self, unit):
        unit.Remove()
        self.Units.remove(unit)

    def DeployUnits(self, filepath, hexMap):
        DOMTree = minidom.parse(filepath)
        #pobieramy elementy struktury dokumentu XML
        cNodes = DOMTree.childNodes

        self.Site = cNodes[0].localName
        if(cNodes[0].localName == "Axis"):
            
            modelInfantry = loader.loadModel("Models/tiger/tiger.egg")
            modelTank = loader.loadModel("Models/tiger/tiger.egg")
            modelPlane = loader.loadModel("Models/fokker/fokker.egg")            
        else:
            modelInfantry = loader.loadModel("Models/churchill/churchill.egg")
            modelTank = loader.loadModel("Models/churchill/churchill.egg")
            modelPlane = loader.loadModel("Models/spitfire/spitfire.egg")

        modelInfantry.setHpr(0,90.0,0)
        modelInfantry.setScale(0.5, 0.5, 0.5)
        modelTank.setHpr(0,90.0,0)
        modelTank.setScale(0.5, 0.5, 0.5)
        modelPlane.setHpr(0,90.0,0)
        modelPlane.setScale(0.5, 0.5, 0.5)

        for i in cNodes[0].childNodes:
            if i.localName == "Infantry":
                    size = int( i.getElementsByTagName("size")[0].childNodes[0].toxml())
                    x = float(i.getElementsByTagName("X")[0].childNodes[0].toxml())
                    y = float(i.getElementsByTagName("Y")[0].childNodes[0].toxml())
                    hex = hexMap.Get(x,y)
                    #

                    infantry = AlliedInfantry(size,hex,self,modelInfantry)
                    self.AddUnit(infantry)

            if i.localName == "Tank":
                    size = int( i.getElementsByTagName("size")[0].childNodes[0].toxml())
                    x = float(i.getElementsByTagName("X")[0].childNodes[0].toxml())
                    y = float(i.getElementsByTagName("Y")[0].childNodes[0].toxml())
                    hex = hexMap.Get(x,y)

                    tank = AlliedInfantry(size,hex,self,modelTank)
                    self.AddUnit(tank)
            if i.localName == "Plane":
                    size = int( i.getElementsByTagName("size")[0].childNodes[0].toxml())
                    x = float(i.getElementsByTagName("X")[0].childNodes[0].toxml())
                    y = float(i.getElementsByTagName("Y")[0].childNodes[0].toxml())
                    hex = hexMap.Get(x,y)

                    plane = AlliedInfantry(size,hex,self,modelPlane)
                    self.AddUnit(plane)
        k=0